package uz.rt.physics;

import junit.framework.TestCase;
import uz.rt.units.Energy;
import uz.rt.units.Length;
import uz.rt.utils.Pair;

public class PddTest extends TestCase
{
// ------------------------------ FIELDS ------------------------------

    final Pdd pdd1;
    final Length.Unit unit = Length.Unit.cm;
    @SuppressWarnings({"HardcodedFileSeparator"})
    final String fn = "/home/tom/IdeaProjects/PlanCheck/inputfiles/pdd_curve_3.txt";
// --------------------------- CONSTRUCTORS ---------------------------

    public PddTest()
    {
        pdd1 = new Pdd();

        pdd1.setEnergy(new Energy(6, Energy.Unit.MV));
        pdd1.add(new Pair<Length, Double>(new Length(10.0, unit), 85.0));
        pdd1.add(new Pair<Length, Double>(new Length(4.6, unit), 123.0));
        pdd1.add(new Pair<Length, Double>(new Length(5.8, unit), 115.0));
        pdd1.add(new Pair<Length, Double>(new Length(10.7, unit), 78.0));
        pdd1.add(new Pair<Length, Double>(new Length(6.5, unit), 110.0));
        pdd1.add(new Pair<Length, Double>(new Length(7.8, unit), 100.0));
        pdd1.add(new Pair<Length, Double>(new Length(8.5, unit), 98.0));
        pdd1.add(new Pair<Length, Double>(new Length(9.0, unit), 92.0));
        pdd1.add(new Pair<Length, Double>(new Length(1.7, unit), 110.0));
        pdd1.add(new Pair<Length, Double>(new Length(3.2, unit), 129.0));
    }

// -------------------------- OTHER METHODS --------------------------

    public void testEdit() throws Exception
    {
        Pdd pdd2 = pdd1;
        Length ol = new Length(1.7, unit);
        Length nl = new Length(1.8, unit);
        pdd2.edit(ol, new Pair<Length, Double>(nl, 111.0));
        assertEquals(111.0, pdd2.get(nl));
        pdd2.edit(0, new Pair<Length, Double>(ol, 110.0));
        assertEquals(110.0, pdd2.get(ol));
    }

    public void testGet() throws Exception
    {
        assertEquals(110.0, pdd1.get(new Length(1.7, unit)));
        assertEquals(88.5, pdd1.get(new Length(9.5, unit)));
        try
        {
            pdd1.get(new Length(0.0, unit));
        }
        catch (ArithmeticException e)
        {
            System.err.println("Caught out of bound parameter");
            System.err.println(e.getMessage());
        }
        try
        {
            pdd1.get(new Length(20.0, unit));
        }
        catch (ArithmeticException e)
        {
            System.err.println("Caught out of bound parameter");
            System.err.println(e.getMessage());
        }
    }

    public void testIsEmpty() throws Exception
    {
        Pdd pdd2 = new Pdd();
        assertEquals(true, pdd2.isEmpty());
        pdd2.add(new Pair<Length, Double>(new Length(1.7, unit), 110.0));
        assertEquals(false, pdd2.isEmpty());
    }

    public void testRead() throws Exception
    {
        CurveIO cio = new CurveIO();
        Pdd pdd3 = new Pdd();
        cio.read(fn, pdd3);
        synchronized (this)
        {
            System.out.println("-- Pdd3 --");
            System.out.println(pdd3);
            assertEquals(true, true);
        }
    }

    public void testWrite() throws Exception
    {
        CurveIO curveIO = new CurveIO();
        curveIO.write(fn, pdd1, unit);
        Pdd pdd2 = null;
        curveIO.read(fn, pdd2);
        assertEquals(pdd1, pdd2);
    }

    public void testRemove() throws Exception
    {
        Pdd pdd2 = pdd1;
        pdd2.sort();
        pdd2.remove(new Length(1.7, unit));
        boolean b = false;
        try
        {
            pdd2.get(new Length(1.7, unit));
        }
        catch (ArithmeticException e)
        {
            b = true;
            System.err.println("Caught out of bound parameter");
            System.err.println(e.getMessage());
        }
        assertEquals(true, b);
    }

    public void testRemoveByKey() throws Exception
    {
        Pdd pdd2 = pdd1;
        pdd2.sort();
        pdd2.removeByKey(0);
        boolean b = false;
        try
        {
            pdd2.get(new Length(1.7, unit));
        }
        catch (ArithmeticException e)
        {
            b = true;
            System.err.println("Caught out of bound parameter");
            System.err.println(e.getMessage());
        }
        assertEquals(true, b);
    }

    public void testScale() throws Exception
    {
        System.out.println("Scale test");
        Pdd pdd2 = pdd1;
        pdd2.sort();
        pdd2.scale();
        System.out.println(pdd2);
    }

    public void testSort() throws Exception
    {
        Pdd pdd2 = new Pdd();
        pdd2.setEnergy(new Energy(6, Energy.Unit.MV));
        pdd2.add(new Pair<Length, Double>(new Length(1.7, unit), 110.0));
        pdd2.add(new Pair<Length, Double>(new Length(3.2, unit), 129.0));
        pdd2.add(new Pair<Length, Double>(new Length(4.6, unit), 123.0));
        pdd2.add(new Pair<Length, Double>(new Length(5.8, unit), 115.0));
        pdd2.add(new Pair<Length, Double>(new Length(6.5, unit), 110.0));
        pdd2.add(new Pair<Length, Double>(new Length(7.8, unit), 100.0));
        pdd2.add(new Pair<Length, Double>(new Length(8.5, unit), 98.0));
        pdd2.add(new Pair<Length, Double>(new Length(9.0, unit), 92.0));
        pdd2.add(new Pair<Length, Double>(new Length(10.0, unit), 85.0));
        pdd2.add(new Pair<Length, Double>(new Length(10.7, unit), 78.0));

        pdd1.sort();
        assertEquals(pdd1, pdd2);
    }
}
